package com.example;

import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @description: 画图工具类
 * @author: zhuangxl 
 * @since: 2021/7/9 16:20
 **/
@Slf4j
public class DrawImageUtil {



    /**
     * @description: 水平旋转图像
     * @author: zhuangxl
     * @since: 2021/7/9 16:23
     * @param originalBufferedImage: 原图
     * @return: java.awt.image.BufferedImage
     **/
    public static BufferedImage horizontalRotate(BufferedImage originalBufferedImage) throws IOException {

        BufferedImage newBufferedImage = null;
        // 旋转角度 水平旋转90度
        Integer angel = 90;

        BigDecimal height = BigDecimal.valueOf(originalBufferedImage.getHeight());
        BigDecimal width = BigDecimal.valueOf(originalBufferedImage.getWidth());
        BigDecimal bd = height.divide(width,4,BigDecimal.ROUND_CEILING);
        BigDecimal theValue = BigDecimal.valueOf(1);

        if (bd.compareTo(theValue) == 1 ) {
            log.info("图片旋转中 ...");
            newBufferedImage = Thumbnails.of(originalBufferedImage).rotate(angel).scale(1).asBufferedImage();
            return newBufferedImage;
        }else {
            log.info("图片不需要旋转, 直接缩放中...");
            return originalBufferedImage;
        }

    }

    /**
     * @description: 垂直旋转图像
     * @author: zhuangxl
     * @since: 2021/7/9 16:23
     * @param originalBufferedImage: 原图
     * @return: java.awt.image.BufferedImage
     **/
    public static BufferedImage verticalRotate(BufferedImage originalBufferedImage) throws IOException {

        BufferedImage newBufferedImage = null;

        // 旋转角度 垂直旋转270度
        Integer angel = 270;

        BigDecimal height = BigDecimal.valueOf(originalBufferedImage.getHeight());
        BigDecimal width = BigDecimal.valueOf(originalBufferedImage.getWidth());
        BigDecimal bd = height.divide(width,4,BigDecimal.ROUND_CEILING);
        BigDecimal theValue = BigDecimal.valueOf(1);

        if (bd.compareTo(theValue) == -1 ) {
            log.info("图片旋转中 ...");
            newBufferedImage = Thumbnails.of(originalBufferedImage).rotate(angel).scale(1).asBufferedImage();
            return newBufferedImage;
        }else {
            log.info("图片不需要旋转");
            return originalBufferedImage;
        }

    }


    /**
     * @description: 图片缩放
     * @author: zhuangxl
     * @since: 2021/7/12 14:46
     * @param bufferedImage: 图片
     * @param maxHeight: 最大高度
     * @param maxWidth: 最大宽度
     * @return: java.awt.image.BufferedImage
     **/
    public static BufferedImage calcScale(BufferedImage bufferedImage, int maxHeight, int maxWidth) throws IOException{

        BigDecimal standardHeight = BigDecimal.valueOf(maxHeight);
        BigDecimal standardWidth = BigDecimal.valueOf(maxWidth);
        BigDecimal height = BigDecimal.valueOf(bufferedImage.getHeight());
        BigDecimal width = BigDecimal.valueOf(bufferedImage.getWidth());

        if (height.compareTo(standardHeight) == 1 || width.compareTo(standardWidth) == 1) {

            log.info("图片缩放中...");

            BigDecimal scaleHeight = standardHeight.divide(height,4,BigDecimal.ROUND_CEILING);
            BigDecimal scaleWidth = standardWidth.divide(width,4,BigDecimal.ROUND_CEILING);

            if (scaleHeight.compareTo(BigDecimal.valueOf(1)) == -1) {
                bufferedImage = Thumbnails.of(bufferedImage).scale(scaleHeight.floatValue()).outputQuality(1.0f).asBufferedImage();
                calcScale(bufferedImage, maxHeight, maxWidth);
            }else if (scaleWidth.compareTo(BigDecimal.valueOf(1)) == -1) {
                bufferedImage = Thumbnails.of(bufferedImage).scale(scaleWidth.floatValue()).outputQuality(1.0f).asBufferedImage();
                calcScale(bufferedImage, maxHeight, maxWidth);
            }
        } else {
            log.info("图片不需要缩放...");
        }

        return  bufferedImage;
    }


}
